package com.nit.lab.dao.Impl;

import com.nit.lab.dao.OrderDao;
import com.nit.lab.dto.DtohisOrder;
import com.nit.lab.model.BaseOrderEntity;
import com.nit.lab.model.BaseOrderFileEntity;
import com.nit.lab.utils.SystemConstants;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class OrderDaoImpl implements OrderDao {

    @Resource
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    @Override
    public Integer creatOrder_file(BaseOrderFileEntity baseOrderFileEntity) throws Exception {
        Integer result = SystemConstants.Insert_Default;
        Session session = this.getSessionFactory().getCurrentSession();
        result = (Integer) session.save(baseOrderFileEntity);
        return result;
    }

    @Override
    public Integer creatOrder(BaseOrderEntity baseOrderEntity, Integer creat) throws Exception {
        Integer result = SystemConstants.Insert_Default;
        Session session = this.getSessionFactory().getCurrentSession();
        if (creat == 0) {
            baseOrderEntity.setOrderStatus(6);
        } else {
            baseOrderEntity.setOrderStatus(0);
        }
        result = (Integer) session.save(baseOrderEntity);
        return result;
    }

    @Override
    public Double getColor(Integer paperColorId) throws Exception {
        Double result;
        String hql = "select paperColorPrice from SysPaperColorEntity where paperColorId  =:paperColorId";
        Session session = this.sessionFactory.getCurrentSession();
        Query query = session.createQuery(hql);
        query.setInteger("paperColorId", paperColorId);
        result = (Double) query.uniqueResult();
        return result;
    }

    @Override
    public Double getSize(Integer paperSizeId) throws Exception {
        Double result;
        String hql = "select paperPrice from SysPaperSizeEntity where paperSizeId  =:paperSizeId";
        Session session = this.sessionFactory.getCurrentSession();
        Query query = session.createQuery(hql);
        query.setInteger("paperSizeId", paperSizeId);
        result = (Double) query.uniqueResult();
        return result;
    }

    @Override
    public Double getType(Integer paperTypeId) throws Exception {
        Double result;
        String hql = "select paperTypePrice from SysPaperTypeEntity where paperTypeId  =:paperTypeId";
        Session session = this.sessionFactory.getCurrentSession();
        Query query = session.createQuery(hql);
        query.setInteger("paperTypeId", paperTypeId);
        result = (Double) query.uniqueResult();
        return result;
    }

    @Override
    public Integer getHisOrderDataTableCount(String search, Integer pre, Integer actorId) throws Exception {
        Integer result = 0;
        String hql;
        Session session = this.sessionFactory.getCurrentSession();
        Query query;
        if (actorId == 3 || actorId == 2) {
            hql = "select count(*) from BaseOrderEntity o, SysStoreEntity s " +
                    "where o.orderStatus != 5 and s.storeId = o.storeId and o.userId =:pre and concat(s.storeName) like '%" + search + "%'";
            query = session.createQuery(hql);
            query.setInteger("pre", pre);
        } else {
            hql = "select count(*) from BaseOrderEntity o, SysStoreEntity s, SysUserEntity sue " +
                    "where o.orderStatus != 5 and s.storeId = o.storeId and sue.userId = o.userId and concat(s.storeName) like '%" + search + "%'";
            query = session.createQuery(hql);
        }
        result = Integer.parseInt(query.uniqueResult().toString());
        return result;
    }

    @Override
    public List<DtohisOrder> getHisOrderDataTable(Integer start, Integer length, String search, Integer pre, Integer actorId) throws Exception {
        List<DtohisOrder> result = null;
        String hql;
        Session session = this.sessionFactory.getCurrentSession();
        Query query;
        if (actorId == 3 || actorId == 2) {
        /*用户,商家，只能看自己的历史订单*/
            hql = "select new com.nit.lab.dto.DtohisOrder(o.orderFiles,o.orderId, o.orderTime,s.storeId, s.storeName, " +
                    "s.storeAddress, s.storeEvaluate, o.orderPay, o.orderStatus, o.orderEvaluate) " +
                    "from BaseOrderEntity o, SysStoreEntity s" +
                    " where o.orderStatus != 5 and s.storeId = o.storeId and o.userId =:pre and concat(s.storeName) like '%" + search + "%'";
            query = session.createQuery(hql);
            query.setInteger("pre", pre);
        } else {
            hql = "select new com.nit.lab.dto.DtohisOrder(o.orderFiles,o.orderId, o.orderTime,s.storeId, s.storeName, " +
                    "s.storeAddress, s.storeEvaluate, o.orderPay, o.orderStatus, o.orderEvaluate) " +
                    "from BaseOrderEntity o, SysStoreEntity s , SysUserEntity sue " +
                    " where o.orderStatus != 5 and s.storeId = o.storeId and sue.userId = o.userId and concat(s.storeName) like '%" + search + "%'";
            query = session.createQuery(hql);
        }
        result = query.setFirstResult(start).setMaxResults(length).list();
        return result;
    }

    @Override
    public Integer chageOrderStatus(Integer orderId, Integer orderStatus) throws Exception {
        String hql;
        Session session = this.sessionFactory.getCurrentSession();
        Query query;
        hql = "update BaseOrderEntity set orderStatus =:orderStatus where orderId=:orderId";
        query = session.createQuery(hql);
        query.setInteger("orderId", orderId);
        query.setInteger("orderStatus", orderStatus);
        return query.executeUpdate();
    }

}
